/**
 * 
 */
package com.mvc.controller.healthmanager;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huangqj.consant.ValidatorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.mvc.model.base.Json;
import com.mvc.model.mb.archive.BaseInfo;
import com.mvc.service.mb.BaseInfoServiceI;

/**
 * @author qingjie
 * 基本信息
 */
@RequestMapping("/baseInfo")
@Controller
public class baseInfoController {
	
	@Autowired
	private BaseInfoServiceI bisi;
	
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public Json SaveBaseInfo(HttpServletRequest request,BaseInfo bi) throws Exception{
		String json = request.getParameter("json");
		System.out.println("未转化数据：" + json);
		//json = URLDecoder.decode(json, "utf-8");
		String[] s = json.split("=");
		if (s.length > 1) {
			json = s[1];
		}
		System.out.println("已转化数据：" + json);
		BaseInfo bInfo = new BaseInfo();
		if (!json.isEmpty()) {
			bInfo = JSON.parseObject(json, BaseInfo.class);
			bInfo.setId(null);
		}
		Json j = new Json();
		if(!ValidatorUtil.isBlankOnString(bInfo.getIdcard())){
			bi = bisi.updateBaseInfo(bInfo);
			if(bi!=null&&!json.isEmpty()){
				if(!bi.getIsHave()){
					j.setMsg("保存成功");
				}else j.setMsg("数据已存在，获取成功");
				j.setSuccess(true);
				j.setObj(bi);
			}else{
				j.setMsg("保存失败");
				j.setSuccess(false);
			}
		}else{
			j.setMsg("身份证号码不能为空");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping(value="/saveOrUpdate", method=RequestMethod.POST,consumes = "application/json")
	@ResponseBody
	public Json SaveBaseInfo(@RequestBody List<BaseInfo> bis,HttpServletRequest request,HttpServletResponse response) throws Exception{
		System.out.println("未转换数据："+JSON.toJSONString(bis));
		Json j = new Json();
		List<BaseInfo> list = new ArrayList<BaseInfo>();
		if(bis.size()>0){
			for(BaseInfo bInfo:bis){
				Long appLocalId = ValidatorUtil.isNullOnLong(bInfo.getId())?null:bInfo.getId();
				BaseInfo bi = new BaseInfo();
				if(!ValidatorUtil.isBlankOnString(bInfo.getIdcard())){
					bInfo.setId(null);
					bi = bisi.updateBaseInfo(bInfo);
					if(bi==null&&ValidatorUtil.isNullOnLong(bi.getId())){
						bi.setRespCode(-1);
					}else{
						if(bi.getIsHave()){
							bi.setRespCode(100);//数据已存在
						}else{
							bi.setRespCode(0);//保存成功
						}
					}
					bi.setAppLocalId(appLocalId);
				}
				list.add(bi);
			}
			j.setMsg("数据已做处理！");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("数据为空");
			j.setSuccess(false);
		}
		return j;
	}
	
	@RequestMapping("/findBaseInfos")
	@ResponseBody
	public Json findBaseInfos (BaseInfo bi){
		Json j = new Json();
		List<BaseInfo> list = bisi.findBaseInfos(bi);
		if(list.size()>0){
			j.setMsg("查询成功！");
			j.setSuccess(true);
			j.setObj(list);
		}else{
			j.setMsg("查询失败!");
			j.setSuccess(false);
		}
		return j;
	}
}
